<?php
namespace App\Models\PartnerModels;
use App\Models\ParentModel;
use App\Components\Tools;
use DB;
/**
 * 合伙人model
 * @author puppet
 */
class Partner extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_partner';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_partner';

  /**;
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;
  
  /**
   * type类型 0.全部 1.合伙人 2.会员
   */
  public function getAllData($type = 0){
    $db = DB::table($this->table)->select('id','user_name','name','mobile')->where('status',1);
    if(intval($type) > 0){
      $db = $db->where('type',$type); 
    }
    return $db->get();
  }
    /**
     * 验证账号
     */
    public function validAdminLogin($id,$password,$type){
        $res = DB::table($this->table)
            ->where('id',$id)
            ->where('pwd', Tools::passwordEncryption($password))
            ->where('type',$type)
            ->where('status','>=',1)
            ->first();
        return !empty($res) ? $res : false;
    }
    /**
     * 验证账号
     */
    public function validAppLogin($name,$password,$type){
        $res = DB::table($this->table)
            ->where('user_name',$name)
            ->where('pwd', Tools::passwordEncryption($password))
            ->where('type',$type)
            ->where('status','>=',1)
            ->first();
        return !empty($res) ? $res : false;
    }
  //获取对应的类型的id
    public function getTypeData($type){
      $sql=" select id,user_name,name,mobile from $this->table where status=1 and type=$type";
      $re=DB::select($sql);
      return isset($re) ? $re:false;
    }
    //移动端接口 获取用户信息 目前就客户端 合伙人的暂时没有
    public function getSelfInfo($id){
      $taocan=$this->selectAppMeal($id);//获取套餐信息
      $selfInfo=$this->appVipInfo($id);//获取用户信息
      $data=array(
          'tc'=>$taocan,
          'selfInfo'=>$selfInfo,
      );
      return isset($data) ? $data:'';
    }
  /**
   * 翻页展示所有信息   合伙人列表
   */
  public function getPageData($params){
    $res = false;
    $limit_num = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
    $limit_sta = 0;
    $page = 1;
    if (isset($params['page']) && intval($params['page']) > 0) {
      $limit_sta = intval($limit_num * intval($params['page'] - 1));
      $page = intval($params['page']);
    }
    $cou_sql = "SELECT count(*) as count FROM {$this->db_table} WHERE type = ? AND `status` >= ?";
    $sql = "SELECT a.createname,a.id,a.city,a.company,a.user_name,a.`name`,a.gender,a.mobile,a.industry_id,a.discount,a.update_time,a.`status`,b.status as realstatus FROM {$this->db_table} as a ";
    $sql.=" left join pt_real_name as b on a.id=b.pid";
    $sql.=" WHERE a.type = ? AND a.`status` >= ?";
    $values = [1,0];
    if(isset($params['user_name']) && !empty($params['user_name'])){
      $cou_sql .= " AND user_name like '%{$params['user_name']}%'";
      $sql .= " AND a.user_name like '%{$params['user_name']}%'";
    }
    if(isset($params['name']) && !empty($params['name'])){
      $cou_sql .= " AND `name` like '%{$params['name']}%'";
      $sql .= " AND a.`name` like '%{$params['name']}%'";
    }
    if(isset($params['mobile']) && !empty($params['mobile'])){
      $cou_sql .= " AND mobile like '%{$params['mobile']}%'";
      $sql .= " AND a.mobile like '%{$params['mobile']}%'";
    }
	if(isset($params['company']) && !empty($params['company'])){
		$cou_sql.=" and company like '%{$params['company']}%'";
		$sql.=" and a.company like '%{$params['company']}%'";
	}
    if(isset($params['order']) && !empty($params['order'])){
      $order_db = ['id'];
      $sql .= " ORDER BY ";
      foreach ($params['order'] as $order){
        $result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
        !empty($result) && $sql .= $result.' '.$order['dir'].',';
      }
      $sql = substr($sql,0,-1);
    }
    $count = DB::select($cou_sql,$values);
    $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
    $sql .= " LIMIT $limit_sta,$limit_num";
    $data = DB::select($sql,$values);
    if(!empty($data)){
		$i=0;
      $oIndustry = new \App\Models\AdminModels\Industry();
      foreach($data as $k => $v){
		$v['kid']=++$i;
          if(empty($v['createname'])){
              $v['createname']='admin';
          }else{
              $data[$k]['createname']=$v['createname'];
          }
        $iData = $oIndustry->db_get(['id'=>['=',$v['industry_id']]]);
        $v['industry_name'] = !empty($iData) ? $iData['name'] : '';
        $v['vips'] = DB::table($this->table)->where('status',1)->where('type',2)->where('fid',$v['id'])->count();
        $data[$k] = $v;
          $data[$k]['vips']="<a href='javascript:;' title='查看会员' aria-label='Update' data-pjax='0' onclick=\"showvips({$v['id']},'{$v['user_name']}')\"><span class='btn btn-info'>{$v['vips']}</span></a>";
          $data[$k]['vips'].="&nbsp;&nbsp;&nbsp;&nbsp;";
        if($v['realstatus']==2){
            $data[$k]['realname']="<button class=\"btn btn btn-info\">实名成功</button>";
        }elseif ($v['realstatus']==1){
            $data[$k]['realname']="<button class=\"btn btn btn-info\">审核中</button>";
        }elseif ($v['realstatus']==0){
            $data[$k]['realname']="<button class=\"btn btn btn-default\">未提交审核</button>";
        }else{
            $data[$k]['realname']="<button class=\"btn btn btn-danger\">实名失败</button>";
        }
      }
    }
    $res['data'] = $data;
    $res['total'] = $count;
    //总条数记录
    return $res;
  }
    //会员列表的收藏数据
    //拉取会员收藏表
    //获取收藏信息
    function getshoucangPageData($id)
    {
        $time = date('Y-m-d', time());
        $sql = " select a.id as aid,b.* from pt_user_collection as a left join pt_dy_hotsearch_video as b on a.vid=b.id where a.pid=$id and a.status=1";
        $sql .= " group by a.vid order by b.comment_count desc ";//排序以及要的数据
        $res = DB::select($sql);
        //  echo '<pre/>';   print_r($res);exit();
        $html = '';
        foreach ($res as $k => $v) {
            if (!empty($v['share_url'])){
                //获取item_ids并输出
                $res=explode('video/',$v['share_url']);
                $pattern =preg_match("/\d+/",$res['1'],$arr);
                $itemid=$arr[0];
                //   print_r($arr);
                $html .= "<tr>";
                $html .= "<td ><div class=\"ant-table-cell\">";
                $html .= "<a href=\"{$v['share_url']}\" target=\'_blank\' style=\"text-decoration:none\" ><div class=\"hot-role clearfix\" ><div class=\"account-box clearfix\" style=\"padding: 10px\"><span class=\" ant-avatar-circle ant-avatar-image avatar\"><img src=\"{$v['cover']}\" style=\"width: 100px;height: 100px\"></span>";
                $html .= "</div></div></a></div></td>";
                $html .= "<td style=\"padding-right: 30px\"><p id=\"text\" >{$v['title']}</p></td>";
                if($v['digg_count']>=10000){
                    $html .= "<td><p id=\"text\" >点赞数：".round($v['digg_count']/10000,1)."<font style=\"color: red\">万</font></p></td>";
                }else{
                    $html .= "<td><p id=\"text\" >点赞数：{$v['digg_count']}</p></td>";
                }
                if($v['comment_count']>=10000){
                    $html .= "<td><p id=\"text\" >评论数：".round($v['comment_count']/10000,1)."<font style=\"color: red\">万</font></p></td>";
                }else{
                    $html .= "<td><p id=\"text\" >评论数：{$v['comment_count']}</p></td>";
                }
                if($v['forward_count']>=10000){
                    $html .= "<td><p id=\"text\" >转发数：".round($v['forward_count']/10000,1)."<font style=\"color: red\">万</font></p></td>";
                }else{
                    $html .= "<td><p id=\"text\" >转发数：{$v['forward_count']}</p></td>";
                }
                if(empty($v['nowatermark_url'])){
                    $html .= "<td><span class=\"btn btn-info\" onclick=\"copyvideo(['$itemid'],{$v['id']})\" style=\"margin-left: 10px\">&nbsp;&nbsp;获取视频链接&nbsp;&nbsp;</span></td>";
                }else{
                    $html .= "<td><a href=\"{$v['nowatermark_url']}\" target=\'_blank\' referrerpolicy=\"no-referrer\" style=\"padding:10px\"><span class=\"btn btn-info\">下载无水印视频</span></a> <a href=\"{$v['voice_url']}\" target=\'_blank\' style=\"padding:10px\"><span class=\"btn btn-info\">下载音频</span></a></td>";
                }
                $html .= "</tr>";

            }
            // echo '<pre/>'; print_r($res);exit();

        }
        return $html;
    }
    // 插入视频区水印链接以及音频链接
    function upVideoto($id, $nowatermark_url, $voice_url)
    {
        $old = $this->userTokenInfo($id);
        $date = date('Y-m-d H:i:s', time());
        // echo "<pre/>";print_r($old);exit;
        $data = array(
            'nowatermark_url' => $nowatermark_url,
            'voice_url' => $voice_url,
            'update_time' => date('Y-m-d H:i:s', time()),
        );
        $sql = "update pt_dy_hotsearch_video set ";
        foreach ($data as $k => $v) {
            $sql .= "`{$k}`='{$v}',";
        }
        $sql = substr($sql, 0, -1) . " where id=$id";
        DB::update($sql);

    }
    //获取用户信息
    function userTokenInfo($id)
    {
        $values = [$id];
        $sql = "select nowatermark_url,voice_url from pt_dy_hotsearch_video where id = ?";
        $re = DB::select($sql, $values);
        return (isset($re) ? $re : '');
    }
	/**
	* 翻页展示所有信息    会员列表
	*/
	public function getVipPageData($params){
		$res = false;
		$size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
		$page = isset($params['page']) ? intval($params['page']) : 1;
		$offset = ($page - 1) * $size;
		$values = [2,0];
		
		$countSql="select count(a.id) as count from {$this->db_table} as a left join pt_industry as b on b.id=a.industry_id left join pt_partner as c on c.id=a.fid";
		$countSql.=" where a.type = ? and a.status >= ?";
		
		$sql="select a.*,b.name as indName,c.user_name as pName,d.status as realstatus  from {$this->db_table} as a left join pt_industry as b on b.id=a.industry_id left join pt_partner as c on c.id=a.fid";
        $sql.=" left join pt_real_name as d on a.id=d.pid";
		$sql.=" where a.type = ? and a.status >= ?";
		
		if(isset($params['user_name']) && !empty($params['user_name'])){
			$countSql .= " AND a.user_name like '%{$params['user_name']}%'";
			$sql .= " AND a.user_name like '%{$params['user_name']}%'";
		}
		if(isset($params['name']) && !empty($params['name'])){
			$countSql .= " AND a.name like '%{$params['name']}%'";
			$sql .= " AND a.name like '%{$params['name']}%'";
		}
		if(isset($params['mobile']) && !empty($params['mobile'])){
			$countSql .= " AND a.mobile like '%{$params['mobile']}%'";
			$sql .= " AND a.mobile like '%{$params['mobile']}%'";
		}
		if(isset($params['company']) && !empty($params['company'])){
			$countSql.=" and a.company like '%{$params['company']}%'";
			$sql.=" and a.company like '%{$params['company']}%'";
		}
		if(isset($params['pName']) && !empty($params['pName'])){
			$countSql.=" and c.user_name like '%{$params['pName']}%'";
			$sql.=" and c.user_name like '%{$params['pName']}%'";
		}
		if(isset($params['order']) && !empty($params['order'])){
			$order_db = ['id'];
			$sql .= " ORDER BY ";
			foreach ($params['order'] as $order){
				$result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
				!empty($result) && $sql .= 'a.'. $result.' '.$order['dir'].',';
			}
			$sql = substr($sql,0,-1);
		}
		$count = DB::select($countSql,$values);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$sql .= " LIMIT $offset,$size";
		$data = DB::select($sql,$values);
		//print_r($data);exit();
		if(!empty($data)){
			$i=0;
			foreach($data as $k=>$v){
				++$i;
				$data[$k]['kid']=$i;
				if(empty($v['createname'])){
                   $v['createname']='admin';
                }else{
                    $data[$k]['createname']=$v['createname'];
				}
                $data[$k]['createname']=$v['createname'];
				$userImg=!empty($v['userImg']) ? $v['userImg'] : '/images/default.png';
				$data[$k]['userImg']="<img src=\"{$userImg}\" width=\"60\" height=\"60\">";
				$data[$k]['sex']=$v['gender']==1 ? '男' : ($v['gender']==2 ? '女' : '未知');
				$data[$k]['date']=$v['start_date']."——".$v['end_date'];
				$data[$k]['status']='';
				$data[$k]['discount']=$v['discount']." 折";
                if($v['realstatus']==2){
                    $data[$k]['realname']="<button class=\"btn btn btn-info\">实名成功</button>";
                }elseif ($v['realstatus']==1){
                    $data[$k]['realname']="<button class=\"btn btn btn-info\">审核中</button>";
                }elseif ($v['realstatus']==0){
                    $data[$k]['realname']="<button class=\"btn btn btn-default\">未提交审核</button>";
                }else{
                    $data[$k]['realname']="<button class=\"btn btn btn-danger\">实名失败</button>";
                }
				if($v['status']==0){
					$data[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"default\" checked onclick=\"actionList({$v['id']},1)\"><span></span></label></div>";
				}elseif($v['status']==1){
					$data[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"checkbox\" checked onclick=\"actionList({$v['id']},0)\"><span></span></label></div>";
				}
                $data[$k]['leida']="<button class=\"btn btn-info\" onclick=\"showleida({$v['id']},'{$v['user_name']}')\">查看雷达信息</button>";
				$data[$k]['operation']="<a href='javascript:;' title='查看' aria-label='View' data-pjax='0'><span class='glyphicon glyphicon-eye-open' onclick=\"viewVip({$v['id']},'{$v['user_name']}')\"></span></a>";
				$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				$data[$k]['operation'].="<a href='/admin/vip/edit/{$v['id']}' title='修改' aria-label='Update' data-pjax='0'><span class='glyphicon glyphicon-pencil'></span></a>";
				$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				$data[$k]['operation'].="<a href='javascript:;' title='删除' aria-label='Delete' data-pjax='0' onclick='actionList({$v['id']},-1)'><span class='glyphicon glyphicon-trash'></span></a>";
				$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
                $data[$k]['operation'].="<a href='/admin/vip/keyword/{$v['id']}' title='查看用户关键词' aria-label='Update' data-pjax='0'><span class='glyphicon glyphicon-screenshot'></span></a>";
                $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
                $data[$k]['operation'].="<a href='javascript:;' title='查看收藏视频' aria-label='Update' data-pjax='0' onclick=\"showshoucang({$v['id']},'{$v['user_name']}')\"><span class='glyphicon glyphicon-star'></span></a>";
                $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
                $data[$k]['operation'].="<a href='javascript:;' title='查看获客情况' aria-label='Update' data-pjax='0' onclick=\"showhuokel({$v['id']},'{$v['user_name']}')\"><span class='fa fa-dashboard'></span></a>";
                $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				$data[$k]['operation'].="<a href='javascript:;' title='套餐详情' aria-label='Delete' data-pjax='0' onclick=\"mealList({$v['id']},'{$v['user_name']}')\"><span class='fa fa-vimeo'></span></a>";
			}
		}
		// echo "<pre/>";print_r($data);exit;
		$res['data'] = $data;
		$res['total'] = $count;
		//总条数记录
		return $res;
	}
	//翻页展示会员同城拓客管理页面
    /**
     * 翻页展示所有信息    会员列表
     */
    public function getVipTuokeData($params){
        $res = false;
        $size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $offset = ($page - 1) * $size;
        $values = [2,0];

        $countSql="select count(a.id) as count from {$this->db_table} as a left join pt_industry as b on b.id=a.industry_id left join pt_partner as c on c.id=a.fid";
        $countSql.=" where a.type = ? and a.status >= ?";

        $sql="select a.*,b.name as indName,c.user_name as pName,d.status as realstatus  from {$this->db_table} as a left join pt_industry as b on b.id=a.industry_id left join pt_partner as c on c.id=a.fid";
        $sql.=" left join pt_real_name as d on a.id=d.pid";
        $sql.=" where a.type = ? and a.status >= ?";

        if(isset($params['user_name']) && !empty($params['user_name'])){
            $countSql .= " AND a.user_name like '%{$params['user_name']}%'";
            $sql .= " AND a.user_name like '%{$params['user_name']}%'";
        }
        if(isset($params['name']) && !empty($params['name'])){
            $countSql .= " AND a.name like '%{$params['name']}%'";
            $sql .= " AND a.name like '%{$params['name']}%'";
        }
        if(isset($params['mobile']) && !empty($params['mobile'])){
            $countSql .= " AND a.mobile like '%{$params['mobile']}%'";
            $sql .= " AND a.mobile like '%{$params['mobile']}%'";
        }
        if(isset($params['company']) && !empty($params['company'])){
            $countSql.=" and a.company like '%{$params['company']}%'";
            $sql.=" and a.company like '%{$params['company']}%'";
        }
        if(isset($params['pName']) && !empty($params['pName'])){
            $countSql.=" and c.user_name like '%{$params['pName']}%'";
            $sql.=" and c.user_name like '%{$params['pName']}%'";
        }
        if(isset($params['order']) && !empty($params['order'])){
            $order_db = ['id'];
            $sql .= " ORDER BY ";
            foreach ($params['order'] as $order){
                $result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
                !empty($result) && $sql .= 'a.'. $result.' '.$order['dir'].',';
            }
            $sql = substr($sql,0,-1);
        }
        $count = DB::select($countSql,$values);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        $sql .= " LIMIT $offset,$size";
        $data = DB::select($sql,$values);
        //print_r($data);exit();
        if(!empty($data)){
            $i=0;
            foreach($data as $k=>$v){
                ++$i;
                $data[$k]['kid']=$i;
                if(empty($v['createname'])){
                    $v['createname']='admin';
                }else{
                    $data[$k]['createname']=$v['createname'];
                }
                $data[$k]['createname']=$v['createname'];
                $userImg=!empty($v['userImg']) ? $v['userImg'] : '/images/default.png';
                $data[$k]['userImg']="<img src=\"{$userImg}\" width=\"60\" height=\"60\">";
                $data[$k]['date']=$v['start_date']."——".$v['end_date'];
                $data[$k]['status']='';
                if($v['status']==0){
                    $data[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"default\" checked onclick=\"actionList({$v['id']},1)\"><span></span></label></div>";
                }elseif($v['status']==1){
                    $data[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"checkbox\" checked onclick=\"actionList({$v['id']},0)\"><span></span></label></div>";
                }
                $data[$k]['operation']="<a href='javascript:;' title='查看拓客活动信息' aria-label='View' data-pjax='0'><span class='glyphicon glyphicon-eye-open' onclick=\"showvipact({$v['id']},'{$v['user_name']}')\"></span></a>";
                $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
                $data[$k]['operation'].="<a href='javascript:;' title='查看拓客用户信息' aria-label='Update' data-pjax='0' onclick=\"showvipuser({$v['id']},'{$v['user_name']}')\"><span class='glyphicon glyphicon-user'></span></a>";
                $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
                $data[$k]['operation'].="<a href='javascript:;' title='查看拓客卡券信息' aria-label='Update' data-pjax='0' onclick=\"showvipcard({$v['id']},'{$v['user_name']}')\"><span class='glyphicon glyphicon-credit-card'></span></a>";
                $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
                $data[$k]['operation'].="<a href='javascript:;' title='查看拓客视频信息' aria-label='Update' data-pjax='0' onclick=\"showvipvideo({$v['id']},'{$v['user_name']}')\"><span class='glyphicon glyphicon-facetime-video'></span></a>";
                $data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }
        // echo "<pre/>";print_r($data);exit;
        $res['data'] = $data;
        $res['total'] = $count;
        //总条数记录
        return $res;
    }

    /**
     * 翻页展示所有信息   获取合伙人旗下的会员
     */
    public function getPartnerVipData($params){
        $res = false;
        $size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 20;
        $page = isset($params['page']) ? intval($params['page']) : 1;
        $offset = ($page - 1) * $size;
        $values = [2,0];
        $id=isset($params['kid']) ? $params['kid']:0;
        $countSql="select count(a.id) as count from {$this->db_table} as a left join pt_industry as b on b.id=a.industry_id left join pt_partner as c on c.id=a.fid";
        $countSql.=" where a.type = ? and a.status >= ? and a.fid=$id";

        $sql="select a.*,b.name as indName,c.user_name as pName,d.status as realstatus  from {$this->db_table} as a left join pt_industry as b on b.id=a.industry_id left join pt_partner as c on c.id=a.fid";
        $sql.=" left join pt_real_name as d on a.id=d.pid";
        $sql.=" where a.type = ? and a.status >= ? and  a.fid=$id";

        if(isset($params['user_name']) && !empty($params['user_name'])){
            $countSql .= " AND a.user_name like '%{$params['user_name']}%'";
            $sql .= " AND a.user_name like '%{$params['user_name']}%'";
        }
        if(isset($params['name']) && !empty($params['name'])){
            $countSql .= " AND a.name like '%{$params['name']}%'";
            $sql .= " AND a.name like '%{$params['name']}%'";
        }
        if(isset($params['mobile']) && !empty($params['mobile'])){
            $countSql .= " AND a.mobile like '%{$params['mobile']}%'";
            $sql .= " AND a.mobile like '%{$params['mobile']}%'";
        }
        if(isset($params['company']) && !empty($params['company'])){
            $countSql.=" and a.company like '%{$params['company']}%'";
            $sql.=" and a.company like '%{$params['company']}%'";
        }
        if(isset($params['pName']) && !empty($params['pName'])){
            $countSql.=" and c.user_name like '%{$params['pName']}%'";
            $sql.=" and c.user_name like '%{$params['pName']}%'";
        }
        if(isset($params['order']) && !empty($params['order'])){
            $order_db = ['id'];
            $sql .= " ORDER BY ";
            foreach ($params['order'] as $order){
                $result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
                !empty($result) && $sql .= 'a.'. $result.' '.$order['dir'].',';
            }
            $sql = substr($sql,0,-1);
        }
        $count = DB::select($countSql,$values);
        $count = isset($count[0]['count']) ? $count[0]['count'] : 0;
        $sql .= " LIMIT $offset,$size";
      //  print_r($sql);exit();
        $data = DB::select($sql,$values);
        $html="";
        if(!empty($data)){
            $i=0;
            foreach($data as $k=>$v){
                ++$i;
                $data[$k]['kid']=$i;
                $html .= "<tr>";
                $html.="<td>{$v['user_name']}</td>";
                $html.="<td>{$v['city']}</td>";
                $html.="<td>{$v['company']}</td>";
                $html.="<td>{$v['indName']}</td>";
                $html.="<td>{$v['name']}</td>";
                if($v['gender']==1){
                    $html.="<td><button class=\"btn btn btn-info\">男</button></td>";
                }elseif ($v['gender']==2){
                    $html.="<td><button class=\"btn btn btn-info\">女</button></td>";
                }else{
                    $html.="<td><button class=\"btn btn btn-info\">未知</button></td>";
                }
                $html.="<td>{$v['mobile']}</td>";
                if($v['realstatus']==2){
                    $html.="<td><button class=\"btn btn btn-info\">实名成功</button></td>";
                }elseif ($v['realstatus']==1){
                    $html.="<td><button class=\"btn btn btn-info\">审核中</button></td>";
                }elseif ($v['realstatus']==0){
                    $html.="<td><button class=\"btn btn btn-default\">未提交审核</button></td>";
                }else{
                    $html.="<td><button class=\"btn btn btn-danger\">实名失败</button></td>";
                }
                if($v['status']==0){
                    $html.="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"default\" checked onclick=\"actionList({$v['id']},1)\"><span></span></label></div></td>";
                }elseif($v['status']==1){
                    $html.="<td><div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"checkbox\" checked onclick=\"actionList({$v['id']},0)\"><span></span></label></div></td>";
                }
                $html .="<td><a href='javascript:;' title='查看' aria-label='View' data-pjax='0'><span class='glyphicon glyphicon-eye-open' onclick=\"viewVip({$v['id']},'{$v['user_name']}')\"></span></a>";
                $html .="&nbsp;&nbsp;&nbsp;&nbsp;";
            $html .="<a href='/admin/vip/edit/{$v['id']}' title='修改' aria-label='Update' data-pjax='0'><span class='glyphicon glyphicon-pencil'></span></a>";
                $html .="&nbsp;&nbsp;&nbsp;&nbsp;";
                $html .="<a href='javascript:;' title='删除' aria-label='Delete' data-pjax='0' onclick='actionList({$v['id']},-1)'><span class='glyphicon glyphicon-trash'></span></a>";
                $html .="&nbsp;&nbsp;&nbsp;&nbsp;";
                $html .="<a href='/admin/vip/keyword/{$v['id']}' title='查看用户关键词' aria-label='Update' data-pjax='0'><span class='glyphicon glyphicon-screenshot'></span></a>";
                $html .="&nbsp;&nbsp;&nbsp;&nbsp;";
                $html .="<a href='javascript:;' title='套餐详情' aria-label='Delete' data-pjax='0' onclick=\"mealList({$v['id']},'{$v['user_name']}')\"><span class='fa fa-vimeo'></span></a></td>";
                $html .= "</tr>";
            }
        }
        //总条数记录
        return $html;
    }
  
	//查询合伙人或者会员 该账户名是否可用
	public function checkUname($uname){
		$resout=array();
		if(!empty($uname)){
			$sql="select user_name from $this->table where user_name=? and status!=-1";
			// echo $sql;exit();
			$resout=DB::select($sql,[$uname]);
		}
		return !empty($resout) ? $resout[0] : false;
	}
	//查询id对应的用户名
    public function getname($id){
	    $sql=" select id,user_name from $this->table where id=$id ";
	    $re=DB::select($sql);
	    return isset($re[0]) ? $re[0]:'';
    }
	//查看合伙人或者会员详情 0 合伙人   1 会员
	public function listOne($data,$type=0){
		$i=0;$values=array();$resout=array();
		if(is_array($data)){
			$sql="select a.*,b.pay_type,b.ca_number,b.remark,d.name as idsname from $this->table as a";
			$sql.=" left join pt_order as b on b.partner_id=a.id";
			if($type){
				$sql.=" left join pt_user as c on c.partner_id=a.id";
			}
			$sql.=" left join pt_industry as d on d.id=a.industry_id";
			$sql.=" where";
			foreach($data as $k=>$v){
				$values[]=$v;
				if($i==0){
					$sql.=" $k=?";
				}else{
					$sql.=" and $k=?";
				}
				$i++;
			}
			$sql.=" limit 1";
			$list=DB::select($sql,$values);
			//print_r($list);exit();
			if(!empty($list)){
				$resout=$list[0];
			}
		}
		return $resout;
	}
	
	//拉取会员套餐详情
	function selectMeal($partner_id){
		$path='/config/inquiry.json';
		$inquiry=\App\Components\Tools::getThird($path);
		$data=DB::select("select * from pt_partner_meal where partner_id = ? and list_type = 1",[$partner_id]);
		$html='';
		if(isset($data[0])){
			//已使用视频上传数
			$num1=DB::select("select count(id) as count from pt_dy_user_video where partner_id=?",[$partner_id]);
			$num1=isset($num1[0]['count']) ? $num1[0]['count'] : 0;
			
			//已使用询盘次数
			$num2=DB::select("select count(id) as count from pt_partner_comment where partner_id=?",[$partner_id]);
			$num2=isset($num2[0]['count']) ? $num2[0]['count'] : 0;
			$vNum=DB::select("select count(id) as count from pt_user_info_comment where partner_id=?",[$partner_id]);
			$vNum=isset($vNum[0]['count']) ? $vNum[0]['count'] : 0;
			$num2=$num2+$vNum;
			
			//已使用视频雷达数
			$num3=DB::select("select count(id) as count from pt_user_info where info_type=2 and system_type=0 and partner_id=?",[$partner_id]);
			$num3=isset($num3[0]['count']) ? $num3[0]['count'] : 0;
			
			//已使用账户雷达数
			$num4=DB::select("select count(id) as count from pt_user_info where system_type=0 and partner_id=? and info_type=1",[$partner_id]);
			$num4=isset($num4[0]['count']) ? $num4[0]['count'] : 0;
			
			foreach($data as $k=>$v){
				$html.="<tr>";
					if($v['list_type']==1){
						$listType='询盘';
						$text="视频上传数：{$v['num1']} 个<br/>询盘次数：{$v['num2']} 次<br/>视频雷达数：{$v['num3']} 个<br/>账户雷达数：{$v['num4']} 个";
					}
					$html.="<td>{$inquiry[$v['meal_name']]['iqname']}</td>";
					$html.="<td>{$v['start']} <br/> {$v['end']}</td>";
					$html.="<td>{$v['price']} 元</td>";
					$html.="<td>{$text}</td>";
				$html.="</tr>";
				$html.="<tr style=\"color:red;\"><td>剩余套餐数</td><td colspan=\"4\" >";
					$html.="视频上传数：".(($v['num1']-$num1)>0 ? ($v['num1']-$num1) : 0)." 个<br/>";
					$html.="询盘次数：".(($v['num2']-$num2)>0 ? ($v['num2']-$num2) : 0)." 次<br/>";
					$html.="视频雷达数：".(($v['num3']-$num3)>0 ? ($v['num3']-$num3) : 0)." 个<br/>";
					$html.="账户雷达数：".(($v['num4']-$num4)>0 ? ($v['num4']-$num4) : 0)." 个";
				$html.="</td></tr>";
			}
		}else{
			$html="<tr><td colspan=\"6\">无套餐记录</td></tr>";
		}
		return $html;
	}
	//拉取移动端的消息详情

    function selectAppMeal($partner_id){
        $time = date('Y-m-d ', time());
        $path='/config/inquiry.json';
        $inquiry=\App\Components\Tools::getThird($path);
        $data=DB::select("select name,meal_name,end,num1 as videoquota,num2 as xunpanquota,num3 as zpquota,num4 as zyquota from pt_partner_meal where partner_id = ? and list_type = 1",[$partner_id]);
      //  print_r($data);exit();
        $html='';
        if(isset($data[0])){
            //已使用视频上传数
            $num1=DB::select("select count(id) as count from pt_dy_user_video where partner_id=?",[$partner_id]);
            $num1=isset($num1[0]['count']) ? $num1[0]['count'] : 0;

            //已使用询盘次数
            $num2=DB::select("select count(id) as count from pt_partner_comment where partner_id=?",[$partner_id]);
            $num2=isset($num2[0]['count']) ? $num2[0]['count'] : 0;
            $vNum=DB::select("select count(id) as count from pt_user_info_comment where partner_id=?",[$partner_id]);
            $vNum=isset($vNum[0]['count']) ? $vNum[0]['count'] : 0;
            $num2=$num2+$vNum;

            //已使用视频雷达数
            $num3=DB::select("select count(id) as count from pt_user_info where info_type=2 and system_type=0 and partner_id=?",[$partner_id]);
            $num3=isset($num3[0]['count']) ? $num3[0]['count'] : 0;

            //已使用账户雷达数
            $num4=DB::select("select count(id) as count from pt_user_info where system_type=0 and partner_id=? and info_type=1",[$partner_id]);
            $num4=isset($num4[0]['count']) ? $num4[0]['count'] : 0;
            //已经使用的时间 先获取对应的
          //  查询对应的id的开始时间  计算时间
            $start=DB::select("select start from pt_partner_meal where  partner_id=? ",[$partner_id]);
            $stime=isset($start[0]['start']) ? $start[0]['start']:$time;

            $days = !empty(round((strtotime($time)-strtotime($stime))/3600/24)) ? round((strtotime($time)-strtotime($stime))/3600/24):1;
            $xl=round($num2/$days);
    //         print_r($days);exit();
            //计算效率
            foreach($data as $k=>$v){
                $data[$k]['spusedcount']=$num1;
                $data[$k]['xunpanusedcount']=$num2;
                $data[$k]['zpusedcount']=$num3;
                $data[$k]['zyusedcount']=$num4;
                $data[$k]['hkxl']=$xl;
            }
        }else{
            $data="无套餐记录";
        }
        return $data;
    }

	//拉取合伙人详情
	function viewInfo($id){
		$sql="select a.*,b.pay_type,b.ca_number from pt_partner as a left join pt_order as b on b.partner_id=a.id";
		$sql.=" where a.id=? and b.partner_type=1 and b.type=3";
		$re=DB::select($sql,[$id]);
		$re=isset($re[0]['id']) ? $re[0] : false;
		$html="";
		if($re){
			$html.="<p>地区：{$re['city']}</p>";
			$html.="<p>合伙机构：{$re['company']}</p>";
			$html.="<p>联系人：{$re['name']}</p>";
			$html.="<p>手机号：{$re['mobile']}</p>";
			$gender=$re['gender']==0 ? "未知" : ($re['gender']==1 ? "男" : "女");
			$html.="<p>性别：{$gender}</p>";
			$html.="<p>地址：{$re['address']}</p>";
			$html.="<p>微信：{$re['wechat']}</p>";
			$html.="<p>邮箱：{$re['email']}</p>";
			$html.="<p>开户比例：{$re['discount']} </p>";
			$html.="<p>开户金额：{$re['first_price']} 元</p>";
			$html.="<p>余额：{$re['balance']} 元</p>";
			$html.="<p>创建时间：{$re['create_time']}</p>";
			$html.="<p>修改时间：{$re['update_time']}</p>";
			if($re['pay_type']==4){
				$html.="<p>对公账号：{$re['ca_number']}</p>";
			}else{
				$config=Config('basics.pay_type');
				$html.="<p>支付方式：{$config[$re['pay_type']]}</p>";
			}
			
		}
		return $html;
	}
	
	//拉取会员详情
	function viewVipInfo($id){
		$sql="select a.*,b.name as indName from pt_partner as a left join pt_industry as b on b.id=a.industry_id";
		$sql.=" where a.type=2 and a.id=?";
		$re=DB::select($sql,[$id]);
		$re=isset($re[0]['id']) ? $re[0] : false;
		$html="";
		if($re){
			$html.="<p>地区：{$re['city']}</p>";
			$html.="<p>会员机构：{$re['company']}</p>";
			$html.="<p>行业：{$re['indName']}</p>";
			$html.="<p>联系人：{$re['name']}</p>";
			$html.="<p>手机号：{$re['mobile']}</p>";
			$gender=$re['gender']==0 ? "未知" : ($re['gender']==1 ? "男" : "女");
			$html.="<p>性别：{$gender}</p>";
			$html.="<p>地址：{$re['address']}</p>";
			$html.="<p>微信：{$re['wechat']}</p>";
			$html.="<p>邮箱：{$re['email']}</p>";
			$html.="<p>创建时间：{$re['create_time']}</p>";
			$html.="<p>修改时间：{$re['update_time']}</p>";
		}
		return $html;
	}
    //拉取会员详情
    function appVipInfo($id){

        $sql="select a.id,a.city,a.company,a.user_name,a.name,a.gender,a.mobile,a.end_date,b.name as indName from pt_partner as a left join pt_industry as b on b.id=a.industry_id";
        $sql.=" where a.type=2 and a.id=?";
        $re=DB::select($sql,[$id]);
   //     print_r($re);exit();
        $re=isset($re[0]['id']) ? $re[0] : false;
        return $re;
    }
	//查询合伙人信息
	function selectParh($id){
		$sql="select * from pt_partner where id=?";
		$re=DB::select($sql,[$id]);
		return isset($re[0]['id']) ? $re[0] : false;
	}
	
	//会员套餐 添加、修改
	function saveTaocan($data,$partner_id){
		if($partner_id){
			$sql="update pt_partner_meal set ";
			foreach($data as $k=>$v){
				$sql.="`".$k."`='".$v."',";
			}
			$sql=substr($sql,0,-1)." where partner_id={$partner_id}";
			DB::update($sql);
		}else{
			DB::table("pt_partner_meal")->insertGetId($data);
		}
	}
	
	//获取会员套餐名称
	function selectMealName($id){
		$sql="select meal_name from pt_partner_meal where partner_id=?";
		$re=DB::select($sql,[$id]);
		return isset($re[0]['meal_name']) ? $re[0]['meal_name'] : '';
	}
	
	//获取会员套餐详情
	function seletMeal($id){
		$sql="select * from pt_partner_meal where partner_id=?";
		$re=DB::select($sql,[$id]);
		return isset($re[0]['meal_name']) ? $re[0] : '';
	}
	
	//获取合伙人列表
	function selectPartner(){
		$sql="select id,company from pt_partner where type=1 and status=?";
		$re=DB::select($sql,[1]);
		return isset($re[0]) ? $re : false;
	}
	
	//查询合伙人余额
	function selectBalance($partner_id){
		$sql="select balance from pt_partner where type=1 and id=?";
		$re=DB::select($sql,[$partner_id]);
		return isset($re[0]['balance']) ? $re[0]['balance'] : false;
	}
	
	//修改合伙人余额
	function upPartnerBalance($partner_id,$newBalance){
		return DB::update("update pt_partner set `balance`={$newBalance} where id={$partner_id}");
	}
	
	//查询指定会员信息
	function selectVip($partner_id){
		$sql="select * from pt_partner where type=2 and id=?";
		$re=DB::select($sql,[$partner_id]);
		return isset($re[0]['id']) ? $re[0] : false;
	}
	
	//合伙人金额变动记录
	function inPriceChange($data){
		DB::table("pt_price_change")->insertGetId($data);
	}
	
	//充值记录
	function inCharging($data){
		DB::table("pt_price_charging")->insertGetId($data);
	}
	
	
	
	
	
	
	
	
	//添加用户关键词
	function addKeyConent($user_id){
		// $url=str_replace("/public",'',$_SERVER['DOCUMENT_ROOT']);
		// $keywords=json_decode(file_get_contents($url."/config/keywords.json"),true);
		$keywords=[
			'多少','怎么买','怎么卖','价','怎么联系','联系方式','怎么合作',
		];
		foreach($keywords as $k=>$v){
			$data=[
				'user_id'=>$user_id,
				'name'=>$v,
				'status'=>1,
				'create_time'=>date('Y-m-d H:i:s'),
				'update_time'=>date('Y-m-d H:i:s'),
			];
			DB::table("pt_keyword_conent")->insertGetId($data);
		}
	}
}